package demo1;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

class Card {
    private String suit;//花色
    private int rank;//数值

    public Card(String suit, int rank) {
        this.suit = suit;
        this.rank = rank;
    }

    public String getSuit() {
        return suit;
    }

    public void setSuit(String suit) {
        this.suit = suit;
    }

    public int getRank() {
        return rank;
    }

    public void setRank(int rank) {
        this.rank = rank;
    }

    @Override
    public String toString() {
        return "[ " + suit + " " + rank + " ]";
    }
}

public class TestCard {

    public static final String[] suits = {"♥", "♠", "♦", "♣"};

    public static List<Card> buyCard() {
        List<Card> desk = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 13; j++) {
                String suit = suits[i];
                Card card = new Card(suit, j);
                desk.add(card);
            }
        }
        return desk;
    }

    public static void shuffle(List<Card> cardList) {
        for(int i = cardList.size()-1;i > 0;i--) {
            Random random = new Random();
            int index = random.nextInt(i);
            swap(cardList, i, index);
        }
    }

    public static void swap(List<Card> cardList, int i, int j) {
        Card tmp = cardList.get(i);
        cardList.set(i, cardList.get(j));
        cardList.set(j, tmp);
    }

    public static void main(String[] args) {
        List<Card> cardList = buyCard();
        System.out.println("买牌：" + cardList);

        shuffle(cardList);
        System.out.println("洗牌：" + cardList);

        List<Card> hand1 = new ArrayList<>();
        List<Card> hand2 = new ArrayList<>();
        List<Card> hand3 = new ArrayList<>();

        List<List<Card>> hands = new ArrayList<>();
        hands.add(hand1);
        hands.add(hand2);
        hands.add(hand3);

        // 3个人轮流揭5张牌
        for (int i = 0; i < 5; i++) {//5轮
            for (int j = 0; j < 3; j++) {//3人一轮
                Card card = cardList.remove(0);
                hands.get(j).add(i, card);
            }
        }

        System.out.println("第一个人的牌：" + hand1);
        System.out.println("第一个人的牌：" + hand2);
        System.out.println("第一个人的牌：" + hand3);
        System.out.println("剩余的牌：" + cardList);

    }


}
